from datetime import datetime
import pyodbc
import pandas as pd

# 连接到SQL Server数据库
conn = pyodbc.connect('''DRIVER={SQL Server};
                      SERVER=192.168.101.31;
                      DATABASE=master;
                      UID=sa;
                      PWD=tangwan1234'''
                      )

# 执行SQL查询
sql_query = "SELECT * FROM dbo.demo"
df = pd.read_sql(sql_query, conn)

# 将结果导出到Excel文件
time_str = datetime.now().strftime("%Y%m%d%H%M%S")
df.to_excel(time_str+".xlsx", index=False)

# 关闭数据库连接
conn.close()
